#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"

#include "wizchip_conf.h"
#include "w5100s.h"
#include "w5100s_spi.h"
#include "socket.h"
#include "loopback.h"
#include "multicast.h"

#define SOCKET_ID 0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)

void network_init(void);

wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x16, 0xed, 0x2f},
    .ip = {192, 168, 1, 11},
    .sn = {255, 255, 255, 0},
    .gw = {192, 168, 1, 1},
    .dns = {8, 8, 8, 8},
    .dhcp = NETINFO_STATIC};

wiz_NetInfo get_info;
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0,};
static uint8_t multicast_mac[6]= {0x01,0x00,0x5e,0x01,0x01,0x0b};   // multicast mac address
static uint8_t multicast_ip[4]  = {224, 1, 1, 11};  // multicast ip address
static uint16_t multicast_port  = 30000; // multicast port

int main()                                                          
{   
    stdio_init_all();
    sleep_ms(2000);
    network_init();

    while(true)
    {
        multicast_loopback(SOCKET_ID, ethernet_buf, multicast_mac, multicast_ip, multicast_port);
        // multicast_recv(SOCKET_ID, ethernet_buf, multicast_mac, multicast_ip, multicast_port);
        sleep_ms(500);
    }
    

}

void network_init(void)
{
    uint8_t temp;
    wizchip_initialize();
    printf("W5100s udp multicast example.\r\n");
    sleep_ms(2000);
    wizchip_setnetinfo(&net_info);
    print_network_information(get_info);
    sleep_ms(2000);   
}